package gptma.fo.controler.employee;

import gptma.bo.model.task.Imputation;
import gptma.bo.model.task.Task;
import gptma.bo.services.employee.EmployeeServices;
import gptma.bo.services.exception.DayLimitReachedException;
import gptma.bo.services.task.TaskServices;
import gptma.fo.cdi.ViewScoped;
import gptma.fo.controler.NavigationRule;
import gptma.fo.controler.WebPage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;
import javax.inject.Named;
import org.primefaces.event.DateSelectEvent;

/**
 * 
 * @author xcl
 */
@Named
@ViewScoped
public class ImputationCreationPage extends WebPage implements Serializable {

	private static final long serialVersionUID = -3855960996419134745L;

	private Task task;

	private Date date;

	private Date dateFrom;

	private Date dateTo;

	private Float etc;

	private boolean finished;

	private String comment;

	private List<Float> consumates;

	private Float consumate;

	private Float consumateInfo;

	private Float taskConsumate;

	private Float taskEtc;

	@EJB
	private EmployeeServices employeeServices;

	@EJB
	private TaskServices taskServices;

	public NavigationRule create() {
		NavigationRule ret = null;

		if (!validate()) {
			return null;
		}

		Imputation imputation = createImputation();

		if (date != null) {
			// single date
			try {
				employeeServices.createImputation(imputation, finished);
				addInfoMessageFromKey("impImputerOk");
				ret = NavigationRule.IMP_WEEKLY;
			} catch (DayLimitReachedException e) {
				addErrorMessageFromKey("impImputerJourneeConsommee");
			}
		} else {
			int count = employeeServices.createMassImputation(imputation,
					dateFrom, dateTo);
			if (count == 0) {
				addInfoMessageFromKey("impImputerOk");
			} else {
				addErrorMessageFromKey("impImputerLoopError", count);
			}

			ret = NavigationRule.IMP_WEEKLY;
		}

		return ret;
	}

	private Imputation createImputation() {
		Imputation imputation = new Imputation();
		imputation.setTask(task);
		imputation.setEmployee(user);
		imputation.setCharge(consumate);
		imputation.setEtc(etc);
		imputation.setComment(comment);

		if (date != null) {
			Calendar cal = Calendar.getInstance(Locale.FRANCE);
			cal.setTime(date);
			cal.set(Calendar.HOUR_OF_DAY, 12);
			imputation.setDate(cal.getTime());
		}

		return imputation;
	}

	private boolean validate() {
		if (date == null && dateFrom == null) {
			addErrorMessageFromKey("impImputerNoDate");
			return false;
		}

		if (date != null && (dateFrom != null || dateTo != null)) {
			addErrorMessageFromKey("impImputerDateDouble");
			return false;
		}

		if ((dateFrom != null && dateTo == null)
				|| (dateFrom == null && dateTo != null)) {
			addErrorMessageFromKey("impImputerPeriodeImputationKO");
			return false;
		}

		return true;
	}

	public void initImputationCreationPage() {
		task = session.getSelectedTask();
		consumates = createConsumates();
		taskConsumate = taskServices.findConsolidatedCharge(task);
		taskEtc = taskServices.getCurrentEtc(task);
	}

	public void dateChanged(DateSelectEvent event) {
		consumateInfo = employeeServices.findConsolidatedCharge(user,
				event.getDate());
	}

	private List<Float> createConsumates() {
		List<Float> ret = new ArrayList<Float>();
		ret.add(0.25f);
		ret.add(0.5f);
		ret.add(0.75f);
		ret.add(1f);

		return ret;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Float getConsumate() {
		return consumate;
	}

	public void setConsumate(Float consumate) {
		this.consumate = consumate;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDateFrom() {
		return dateFrom;
	}

	public void setDateFrom(Date dateFrom) {
		this.dateFrom = dateFrom;
	}

	public Date getDateTo() {
		return dateTo;
	}

	public void setDateTo(Date dateTo) {
		this.dateTo = dateTo;
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public Float getEtc() {
		return etc;
	}

	public void setEtc(Float etc) {
		this.etc = etc;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Float getConsumateInfo() {
		return consumateInfo;
	}

	public List<Float> getConsumates() {
		return consumates;
	}

	public Float getTaskConsumate() {
		return taskConsumate;
	}

	public Float getTaskEtc() {
		return taskEtc;
	}

}
